﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GRV11;

namespace gpuimp.core
{
    [Serializable]
    public class MirrorPlane : Modifier
    {
        public MirrorPlane()
        {
            fragPass1 = null;
            fragPass2 = null;
            vertProg = null;

            id = -1;
            Name = "Mirror Plane";
            shaderPass1 = shaderPass2 = -1;

            setFragProg("");
            setVertProg("");

            twoPass = true;

            setupShader();
        }

        public void setCopyShader()
        {
            GRSupport.SupportShaderProgramSelect(CanvasManager.gr, Tool.copyShader);

            int shaderDiffuse = (int)CanvasManager.gr.glGetUniformLocationARB(Tool.copyShader, "diffuseMap");
            CanvasManager.gr.glUniform1iARB(shaderDiffuse, 0);
        }

        public void setupToolViewport(GR gr)
        {
            // Viewport 
            Point imageSize = CanvasManager.CurrentCanvas.ImageSize;
            // Viewport to same size as the texture
            gr.glViewport(0, 0, imageSize.X, imageSize.Y);

            // Clear the viewport
            gr.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            gr.glClear(GR.GL_COLOR_BUFFER_BIT | GR.GL_DEPTH_BUFFER_BIT);

            // PROJECTION matrix
            gr.glMatrixMode(GR.GL_PROJECTION);
            gr.glLoadIdentity();
            double aspectRatio = 1.0;
            if (0 != imageSize.Y)
                aspectRatio = ((double)(imageSize.X) / (double)(imageSize.Y));

            int halfWidth = imageSize.X;// / 2;
            int halfHeight = imageSize.Y;// / 2;
            gr.gluOrtho2D(-halfWidth, halfWidth, -halfHeight, halfHeight);

            // MODELVIEW matrix, typically used to transform individual models
            gr.glMatrixMode(GR.GL_MODELVIEW);
            gr.glLoadIdentity();
        }

        public override void ActivatePass1()
        {
            GRSupport.SupportShaderProgramSelect(CanvasManager.gr, this.shaderPass1);

            int shaderDiffuse = (int)CanvasManager.gr.glGetUniformLocationARB(this.shaderPass1, "Texture0");
            int shaderMirror = (int)CanvasManager.gr.glGetUniformLocationARB(this.shaderPass1, "mirrorPos");
            CanvasManager.gr.glUniform1iARB(shaderDiffuse, 0);
            CanvasManager.gr.glUniform1fARB(shaderMirror, 0.33f);
        }

        public override void ActivatePass2()
        {
        }

        public override void Deactivate()
        {
            GRSupport.SupportShaderProgramSelect(CanvasManager.gr, 0);
        }

        public override void setFragProg(String frag)
        {
            fragPass1 = @"
uniform sampler2D Texture0;
uniform float mirrorPos;

varying vec2 texCoord;

void main(void)
{
mirrorPos = 1 - mirrorPos;
   if(texCoord.x > mirrorPos) 
        texCoord.x = mirrorPos + (mirrorPos - texCoord.x);
    gl_FragColor = texture2D( Texture0, texCoord );
}
";

            fragPass2 = @"";
        }


        public override void setVertProg(String vert)
        {
            //this.vertProg = vertProg;
            vertProg = @"
varying vec2  texCoord;
void main(void)
{
   gl_Position = ftransform();
   texCoord = gl_MultiTexCoord0.xy;
}
";
        }
    }
}
